#include "onehot_custom_tiling.h"
#include "kernel_operator.h"

constexpr int32_t BUFFER_NUM = 2;
constexpr uint32_t LAST_TWO_TILE = 2;

class KernelOneHot {
public:
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR depth, GM_ADDR z, const OnehotCustomTilingData tiling) {
        this->DEPTH = tiling.numClass;
        //this->mask_cmp = (uint64_t)DEPTH;
        this->DEPTH_align = ((DEPTH + 8 - 1) / 8) * 8;
        this->mask_cmp = DEPTH_align;
        if (tiling.isEvenCore)
        {
            this->blockLength = tiling.blockLength;
            this->tileNum = tiling.tileNum;
            this->tileLength = tiling.tileLength / BUFFER_NUM;
            this->lastTileLength = tiling.lastTileLength;

            xGm.SetGlobalBuffer((__gm__ int32_t *)x + this->blockLength * AscendC::GetBlockIdx(), this->blockLength);
            dGm.SetGlobalBuffer((__gm__ int32_t *)depth,DEPTH_align);
            zGm.SetGlobalBuffer((__gm__ int32_t *)z + this->blockLength*DEPTH * AscendC::GetBlockIdx(), this->blockLength*DEPTH);
            
        }
        else
        {
            if (AscendC::GetBlockIdx() < tiling.formerNum)
            {
                this->tileNum = tiling.formerTileNum;
                this->tileLength = tiling.formerTileLength / BUFFER_NUM;
                this->lastTileLength = tiling.formerLastTileLength;

                xGm.SetGlobalBuffer((__gm__ int32_t *)x + tiling.formerLength * AscendC::GetBlockIdx(),
                                    tiling.formerLength);
                dGm.SetGlobalBuffer((__gm__ int32_t *)depth,DEPTH_align);
                zGm.SetGlobalBuffer((__gm__ int32_t *)z + tiling.formerLength*DEPTH * AscendC::GetBlockIdx(),
                                    tiling.formerLength*DEPTH);
            }
            else
            {
                this->tileNum = tiling.tailTileNum;
                this->tileLength = tiling.tailTileLength / BUFFER_NUM;
                this->lastTileLength = tiling.tailLastTileLength;

                xGm.SetGlobalBuffer((__gm__ int32_t *)x + tiling.formerLength * tiling.formerNum +
                                        tiling.tailLength * (AscendC::GetBlockIdx() - tiling.formerNum),
                                    tiling.tailLength);
                dGm.SetGlobalBuffer((__gm__ int32_t *)depth,DEPTH_align);
                zGm.SetGlobalBuffer((__gm__ int32_t *)z + tiling.formerLength * tiling.formerNum*DEPTH +
                                        tiling.tailLength*DEPTH * (AscendC::GetBlockIdx() - tiling.formerNum),
                                    tiling.tailLength*DEPTH);

            }
        }
        pipe.InitBuffer(cmpBuf, 8*DEPTH_align * sizeof(uint8_t));

        //pipe.InitBuffer(zeroBuf, TILE_LENGTH*DEPTH * sizeof(float));
        pipe.InitBuffer(oneBuf, DEPTH_align * sizeof(float));
        pipe.InitBuffer(zBuf, DEPTH_align * sizeof(float));

        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileLength * sizeof(int32_t));
        pipe.InitBuffer(inQueueD, BUFFER_NUM, DEPTH_align * sizeof(int32_t));
        pipe.InitBuffer(outQueueZ, BUFFER_NUM, DEPTH_align * sizeof(int32_t));
    }

    __aicore__ inline void Process()
    {
        int32_t loopCount = this->tileNum * BUFFER_NUM;
        for (int32_t i = 0; i < loopCount; i++)
        {
            for(int j=0;j<this->tileLength;j++){
                CopyInD();
                CopyIn(i,j);
                Compute(i,j);
                CopyOut(i,j);
            }
        }
    }

private:

    __aicore__ inline void CopyInD()
    {
        AscendC::LocalTensor<int32_t> dLocal = inQueueD.AllocTensor<int32_t>();
        AscendC::DataCopy(dLocal, dGm, DEPTH_align);
        for(int i=DEPTH;i<DEPTH_align;i++)
        {
            dLocal.SetValue(i, -1);
        }
        inQueueD.EnQue(dLocal);
    }

    __aicore__ inline void CopyIn(int32_t progress,int32_t j)
    {
        AscendC::LocalTensor<int32_t> xLocal = inQueueX.AllocTensor<int32_t>();
        if ((progress == (this->tileNum * BUFFER_NUM - 2)) || (progress == (this->tileNum * BUFFER_NUM - 1)))
        {
            AscendC::DataCopy(xLocal, xGm[(progress - LAST_TWO_TILE) * this->tileLength + this->lastTileLength],
                              this->tileLength);
        }
        else
        {
            AscendC::DataCopy(xLocal, xGm[progress * this->tileLength], this->tileLength);
        }
        inQueueX.EnQue(xLocal);
    }

    __aicore__ inline void Compute(int32_t progress,int32_t j) {
        AscendC::LocalTensor<int32_t> xLocal = inQueueX.DeQue<int32_t>();
        AscendC::LocalTensor<int32_t> dLocal = inQueueD.DeQue<int32_t>();
        AscendC::LocalTensor<int32_t> zLocal = outQueueZ.AllocTensor<int32_t>();
        AscendC::LocalTensor<uint8_t> cmp = cmpBuf.AllocTensor<uint8_t>();
        AscendC::LocalTensor<float> zfloat = zBuf.AllocTensor<float>();
        AscendC::LocalTensor<float> oneLocal = oneBuf.AllocTensor<float>();
        AscendC::Duplicate(oneLocal, 1.0f, DEPTH_align);

        int32_t xVal = xLocal.GetValue(j);
        AscendC::CompareScalar(cmp, dLocal, xVal, AscendC::CMPMODE::EQ, mask_cmp, repeat, repeatParams);
        AscendC::LocalTensor<uint16_t> mask_Dup = cmpBuf.AllocTensor<uint16_t>();
        AscendC::Select(zfloat, mask_Dup[0], oneLocal, 0.0f, AscendC::SELMODE::VSEL_TENSOR_SCALAR_MODE, DEPTH_align);
        AscendC::Cast(zLocal, zfloat, AscendC::RoundMode::CAST_CEIL,DEPTH_align);

        outQueueZ.EnQue<int32_t>(zLocal);
        inQueueX.FreeTensor(xLocal);
        inQueueD.FreeTensor(dLocal);
        cmpBuf.FreeTensor(cmp);
        oneBuf.FreeTensor(oneLocal);
        zBuf.FreeTensor(zfloat);
    }

    /*__aicore__ inline void CopyOut(int32_t progress,int32_t j)
    {
        AscendC::LocalTensor<int32_t> zLocal = outQueueZ.DeQue<int32_t>();
        AscendC::DataCopyExtParams copyParams{static_cast<uint16_t>(1), static_cast<uint32_t>(DEPTH * sizeof(int32_t)), 0, 0, 0};
        if ((progress == (this->tileNum * BUFFER_NUM - 2)) || (progress == (this->tileNum * BUFFER_NUM - 1)))
        {
            AscendC::DataCopyPad(zGm[(progress - LAST_TWO_TILE) * this->tileLength*DEPTH + this->lastTileLength*DEPTH], zLocal,
                              copyParams);
        }
        else
        {
            AscendC::DataCopyPad(zGm[progress * this->tileLength*DEPTH], zLocal, copyParams);
        }
        outQueueZ.FreeTensor(zLocal);
    }*/

    __aicore__ inline void CopyOut(int32_t progress,int32_t j)
    {
        AscendC::LocalTensor<int32_t> zLocal = outQueueZ.DeQue<int32_t>();
        AscendC::DataCopyExtParams copyParams{static_cast<uint16_t>(1), static_cast<uint32_t>(DEPTH * sizeof(int32_t)), 0, 0, 0};
        if ((progress == (this->tileNum * BUFFER_NUM - 2)) || (progress == (this->tileNum * BUFFER_NUM - 1)))
        {
            AscendC::DataCopyPad(zGm[(progress - LAST_TWO_TILE) * this->tileLength*DEPTH + (this->lastTileLength+j)*DEPTH], zLocal,
                              copyParams);
        }
        else
        {
            AscendC::DataCopyPad(zGm[(progress * this->tileLength+j)*DEPTH], zLocal, copyParams);
        }
        outQueueZ.FreeTensor(zLocal);
    }

private:
    AscendC::TPipe pipe;
    AscendC::TQue<AscendC::TPosition::VECIN,  BUFFER_NUM> inQueueX;
    AscendC::TQue<AscendC::TPosition::VECIN, BUFFER_NUM> inQueueD;
    AscendC::TQue<AscendC::TPosition::VECOUT, BUFFER_NUM> outQueueZ;

    AscendC::TBuf<AscendC::QuePosition::VECCALC> cmpBuf;
    AscendC::TBuf<AscendC::QuePosition::VECCALC> zeroBuf;
    AscendC::TBuf<AscendC::QuePosition::VECCALC> oneBuf;
    AscendC::TBuf<AscendC::QuePosition::VECCALC> zBuf;

    AscendC::GlobalTensor<int32_t> xGm;
    AscendC::GlobalTensor<int32_t> dGm;
    AscendC::GlobalTensor<int32_t> zGm;

    uint64_t mask_cmp;
    int repeat = 1;
    AscendC::UnaryRepeatParams repeatParams = { 1, 1, 8, 8 };
    
    uint32_t DEPTH;;
    uint32_t blockLength;
    uint32_t tileNum;
    uint32_t tileLength;
    uint32_t lastTileLength;

    int32_t DEPTH_align;
};

extern "C" __global__ __aicore__ void onehot_custom(GM_ADDR x,GM_ADDR depth, GM_ADDR z, OnehotCustomTilingData tiling) {
    KernelOneHot op;
    op.Init(x,depth, z, tiling);
    op.Process();
}
